<?php
namespace app\controller;
use app\BaseController;
use app\model\Assets;
use app\model\Advertisements;
use think\facade\Filesystem;

class Ad extends BaseController {

  function batchRemove(){
    $ids = $this->request->param('ids');
    $service = new Advertisements();
    $data = $service->batchRemoveEntity($ids);
    return ResponseResult($data); 
  }

  function remove(){
    $aid = $this->request->param("aid");
    $service = new Advertisements();
    $data = $service->removeEntity($aid);
    return ResponseResult($data);
  }

  /**
   * 更新广告
   */
  function update(){
    //获取资源的ID
    $file = $this->request->file("file");
    $assetId = $this->request->post("assetId");
    //如果还没有上传图片的话，说明此次不需要更新图片
    if(!isNullOrEmpty($file)) {
      $type = preg_match("/(jpe?g)|(png)|(gif)|(webp)/i", $file->extension()) ? 1 : 0;
      $root = "public/storage";
      $savename = Filesystem::disk('public')->putFile( 'assets', $file);
      $absPath = $root.$savename;
      $name = $file -> getOriginalName();
      $obj = [
        'name' => $name,
        'assetType' => $type,
        'absPath' => $absPath,
        'serverPath' => '/storage/'.$savename,
        'purpose' => 'advertisement'
      ];
      $assetQuery = new Assets();
      //先将之前的图片删除
      $prev = $assetQuery->findOrEmpty($assetId);
      if(!$prev->isEmpty()) {
        $prev->delete();
      }
      //再保存新的图片
      $assets = (Object)$assetQuery->saveEntity((Object)$obj);
      $assetId = $assets->id;
    }
    $adId = $this->request->post("id");
    $productId = $this->request->post("productId");
    $description = $this->request->post("description");
    $state = $this->request->post("state");
    $payload = [
      'id' => $adId,
      'assetId' => $assetId,
      'productId' => $productId,
      'description' => $description,
      "state" => $state,
    ];
    $query = new Advertisements();
    $data = $query->updateEntity($adId, (Object) $payload);
    return ResponseResult($data);
  }


  /**
   * 保存广告
   */
  function create(){
    $assetId = $this->request->post("assetId");
    //如果还没有保存的话
    if(!isNullOrEmpty($assetId)) {
      //如果还没有保存，先保存图片
      $file = $this->request->file("file");
      $type = preg_match("/(jpe?g)|(png)|(gif)|(webp)/i", $file->extension()) ? 1 : 0;
      $root = "public/storage";
      $savename = Filesystem::disk('public')->putFile( 'assets', $file);
      $absPath = $root.$savename;
      $name = $file -> getOriginalName();
      $obj = [
        'name' => $name,
        'assetType' => $type,
        'absPath' => $absPath,
        'serverPath' => '/storage/'.$savename,
        'purpose' => 'advertisement'
      ];
      $assetQuery = new Assets();
      $assets = (Object)$assetQuery->saveEntity((Object)$obj);
      $assetId = $assets->id;
    }
    $productId = $this->request->post("productId");
    $description = $this->request->post("description");
    $model = new Advertisements();
    //默认产品的状态是可用状态
    $payload = [
      'assetId' => $assetId,
      'productId' => $productId,
      'description' => $description,
      "state" => 1,
    ];
    $data = $model->saveEntity((Object) $payload);
    return ResponseResult($data);
  }

  /**
   * 获取所有的广告列表
   */
  function getPagination($productName = '', $capacity = 15, $offset = 1) {
    $query = new Advertisements();
    $data = $query->getPagination($productName, $capacity, $offset);
    return ResponseResult($data);
  }

  /**
   * 获取已经绑定的产品集合
   */
  function getBindProducts(){
    $query = new Advertisements();
    $data = $query->getBindProducts();
    return ResponseResult($data);
  }

  function changeState($adId){
    $query = new Advertisements();
    $data = $query->updateEntityState($adId);
    return ResponseResult($data);
  }

  function getNavAdvertisement(){
    $service = new Advertisements();
    $data = $service->getAvailableAdvertisement();
    return ResponseResult($data);
  }

}